//
// Copyright (C) [2024] Xingyun Integrated Circuit, Inc.
//
// GreenCode was a private technology asset of Xingyun Integrated Circuit， Inc （Confidential）
//  Author: Shawn.Tan
//  Date : 2025.10.28
//
//  History : Initial Version 2025.10.28
//
//
#ifndef Green_PyStateTransition_H
#define Green_PyStateTransition_H

#include "pybind11/pybind11.h"
#include "pybind11/stl.h"

#include "GenRequest.h"
#include "Generator.h"
#include "Scheduler.h"
#include "StateTransition.h"
#include "ThreadContext.h"

namespace py = pybind11;

namespace Green {

  PYBIND11_MODULE(StateTransition, mod) {
    // Need to import the Enums module in order to specify a default state transition order mode.
    py::module::import("Enums");

    std::vector<EStateElementType> empty_elem_type_order;

    mod
      .def("registerStateTransitionHandler",
        [](const py::object stateTransHandler, const EStateTransitionType stateTransType, const std::vector<EStateElementType>& rStateElemTypes) {
          ThreadContext thread_context;

          StateTransitionManagerRepository* state_trans_manager_repo = StateTransitionManagerRepository::Instance();
          StateTransitionManager* state_trans_manager = state_trans_manager_repo->GetStateTransitionManager(thread_context.GetThreadId());
          state_trans_manager->RegisterStateTransitionHandler(stateTransHandler, stateTransType, rStateElemTypes);
        })
      .def("setDefaultStateTransitionHandler",
        [](const py::object stateTransHandler, const EStateElementType stateElemType) {
          ThreadContext thread_context;

          StateTransitionManagerRepository* state_trans_manager_repo = StateTransitionManagerRepository::Instance();
          StateTransitionManager* state_trans_manager = state_trans_manager_repo->GetStateTransitionManager(thread_context.GetThreadId());
          state_trans_manager->SetDefaultStateTransitionHandler(stateTransHandler, stateElemType);
        })
      .def("setDefaultStateTransitionOrderMode",
        [](const EStateTransitionType stateTransType, const EStateTransitionOrderMode orderMode, const std::vector<EStateElementType>& rStateElemTypeOrder) {
          ThreadContext thread_context;

          StateTransitionManagerRepository* state_trans_manager_repo = StateTransitionManagerRepository::Instance();
          StateTransitionManager* state_trans_manager = state_trans_manager_repo->GetStateTransitionManager(thread_context.GetThreadId());
          state_trans_manager->SetDefaultStateTransitionOrderMode(stateTransType, orderMode, rStateElemTypeOrder);
        },
        py::arg(), py::arg(), py::arg("aStateElemTypeOrder") = empty_elem_type_order)
      .def("transitionToState",
        [](const State& rState, const EStateTransitionOrderMode orderMode, const std::vector<EStateElementType>& rStateElemTypeOrder) {
          ThreadContext thread_context;

          Scheduler* scheduler = Scheduler::Instance();
          Generator* generator = scheduler->LookUpGenerator(thread_context.GetThreadId());
          generator->GenSequence(new GenStateTransitionRequest(&rState, EStateTransitionType::Explicit, orderMode, rStateElemTypeOrder));
        },
        py::arg(), py::arg("aOrderMode") = EStateTransitionOrderMode::UseDefault, py::arg("aStateElemTypeOrder") = empty_elem_type_order)
      ;
  }

}

#endif  // Green_PyStateTransition_H
